﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Windows.Forms.AxHost;

namespace WinFormsAppTest.Dao
{
    internal class ExamDao
    {

        //使用hyl版本进行登录
        //String connectionString = @"Data Source=DESKTOP-SQERB5L\MSSQLSERVERR;" +
        //    "Initial Catalog=hylpersonsystem;" +
        //    "User ID=hyl;" +
        //    "Password=123456;";

        //使用window登录
        String connectionString = @"Data Source=DESKTOP-SQERB5L\MSSQLSERVERR;" +
            "Initial Catalog=hylpersonsystem;" +
            "Integrated Security=true;";



        //connect 无参
        public SqlConnection connect()
        {

            using (SqlConnection sc = new SqlConnection(connectionString))
            {  ///
                sc.Open();
                try
                {
                    //sc.Open();

                    // 检查连接状态
                    if (sc.State == ConnectionState.Open)
                    {
                        Console.WriteLine("数据库连接成功！");
                        MessageBox.Show("数据库连接成功！");
                        sc.Close();

                        // 连接成功后可以执行其他操作
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("数据库连接失败：" + ex.Message);
                    MessageBox.Show("数据库连接失败！");
                    sc.Close();
                }
                return sc;
            }///
        }
        //


        //添加人事考核奖惩信息
        public void CreateExam(string state, string measure,
                               string goal, int userid)
        {
            // 在这里执行具体的数据库操作
            // 例如使用 SqlCommand 执行查询语句或执行存储过程
            SqlConnection sc = new SqlConnection(connectionString);
            sc.Open();
            String sql = "insert into exam values(@State,@Measure,@Goal,@Userid)";
            if (sc.State != ConnectionState.Open)
            {
                MessageBox.Show("注册时，数据库连接失败！");
                return;
            }
            using (SqlCommand command = new SqlCommand(sql, sc))
            {
                command.Parameters.AddWithValue("@State", state);
                command.Parameters.AddWithValue("@Measure", measure);
                command.Parameters.AddWithValue("@Goal", goal);
                command.Parameters.AddWithValue("@Userid", userid);
                command.ExecuteNonQuery();
                sc.Close();
                return;
            }
        }

        //根据人事考核奖惩id删除人事考核奖惩信息
        public void DeleteExamById(int id)
        {
            // 在这里执行具体的数据库操作
            // 例如使用 SqlCommand 执行查询语句或执行存储过程
            SqlConnection sc = new SqlConnection(connectionString);
            sc.Open();
            String sql = "delete from exam where examid=@ExamId";
            if (sc.State != ConnectionState.Open)
            {
                MessageBox.Show("删除时，数据库连接失败！");
                return;
            }
            using (SqlCommand command = new SqlCommand(sql, sc))
            {
                command.Parameters.AddWithValue("@ExamId", id);
                command.ExecuteNonQuery();
                sc.Close();
                return;
            }
        }


        //根据考核奖惩id更新人事考核奖惩信息
        public void UpdateExamById(int id, string state, 
                                  string measure,string goal)
        {
            // 在这里执行具体的数据库操作
            // 例如使用 SqlCommand 执行查询语句或执行存储过程
            SqlConnection sc = new SqlConnection(connectionString);
            sc.Open();
            String sql = "update exam set state=@State, measure=@Measure, goal=@Goal where examid=@ExamId";
            if (sc.State != ConnectionState.Open)
            {
                MessageBox.Show("更新时，数据库连接失败！");
                return;
            }
            using (SqlCommand command = new SqlCommand(sql, sc))
            {
                command.Parameters.AddWithValue("@State", state);
                command.Parameters.AddWithValue("@Measure", measure);
                command.Parameters.AddWithValue("@Goal", goal);
                command.Parameters.AddWithValue("@ExamId", id);
                command.ExecuteNonQuery();
                sc.Close();
                return;
            }
        }


        //根据考核id获取用户全部人员考核奖惩信息
        public List<WinFormsAppTest.pojo.Exam> GetExamById(int id)
        {
            SqlConnection sc = new SqlConnection(connectionString);
            sc.Open();
            String sql = "select * from exam where examid = @Examid";
            if (sc.State != ConnectionState.Open)
            {
                MessageBox.Show("打开时，数据库连接失败！");
                return null;
            }
            using (SqlCommand command = new SqlCommand(sql, sc))
            {
                command.Parameters.AddWithValue("@Examid", id);
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    List<WinFormsAppTest.pojo.Exam> exams = new List<WinFormsAppTest.pojo.Exam>();
                    while (reader.Read())
                    {
                        // 处理查询结果
                        // 例如输出数据到控制台或存储到集合中
                        int examid = reader.GetInt32(0);
                        string state = reader.GetString(1);
                        string measure = reader.GetString(2);
                        string goal = reader.GetString(3);
                        int userid = reader.GetInt32(4);
                        WinFormsAppTest.pojo.Exam f = new WinFormsAppTest.pojo.Exam(examid, state, measure, goal,userid);
                        exams.Add(f);
                    }
                    sc.Close();
                    return exams;
                }
            }
        }
        //

        //根据用户id获取用户全部人员考核奖惩信息
        public List<WinFormsAppTest.pojo.Exam> GetExamByUserId(int id)
        {
            SqlConnection sc = new SqlConnection(connectionString);
            sc.Open();
            String sql = "select * from exam where userid = @Userid";
            if (sc.State != ConnectionState.Open)
            {
                MessageBox.Show("打开时，数据库连接失败！");
                return null;
            }
            using (SqlCommand command = new SqlCommand(sql, sc))
            {
                command.Parameters.AddWithValue("@Userid", id);
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    List<WinFormsAppTest.pojo.Exam> exams = new List<WinFormsAppTest.pojo.Exam>();
                    while (reader.Read())
                    {
                        // 处理查询结果
                        // 例如输出数据到控制台或存储到集合中
                        int examid = reader.GetInt32(0);
                        string state = reader.GetString(1);
                        string measure = reader.GetString(2);
                        string goal = reader.GetString(3);
                        int userid = reader.GetInt32(4);
                        WinFormsAppTest.pojo.Exam f = new WinFormsAppTest.pojo.Exam(examid, state, measure, goal, userid);
                        exams.Add(f);
                    }
                    sc.Close();
                    return exams;
                }
            }
        }
        //

        //根据奖惩情况信息获取用户全部考核奖惩信息
        public List<WinFormsAppTest.pojo.Exam> GetExamByState(string _state)
        {
            SqlConnection sc = new SqlConnection(connectionString);
            sc.Open();
            String sql = "select * from exam where state = @State";
            if (sc.State != ConnectionState.Open)
            {
                MessageBox.Show("打开时，数据库连接失败！");
                return null;
            }
            using (SqlCommand command = new SqlCommand(sql, sc))
            {
                command.Parameters.AddWithValue("@State", _state);
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    List<WinFormsAppTest.pojo.Exam> exams = new List<WinFormsAppTest.pojo.Exam>();
                    while (reader.Read())
                    {
                        // 处理查询结果
                        // 例如输出数据到控制台或存储到集合中
                        int examid = reader.GetInt32(0);
                        string state = reader.GetString(1);
                        string measure = reader.GetString(2);
                        string goal = reader.GetString(3);
                        int userid = reader.GetInt32(4);
                        WinFormsAppTest.pojo.Exam f = new WinFormsAppTest.pojo.Exam(examid, state, measure, goal, userid);
                        exams.Add(f);
                    }
                    sc.Close();
                    return exams;
                }
            }
        }
        //

        //通过获取用户全部人事考核奖惩信息
        public List<WinFormsAppTest.pojo.Exam> GetExams()
        {
            SqlConnection sc = new SqlConnection(connectionString);
            sc.Open();
            String sql = "select * from exam";
            if (sc.State != ConnectionState.Open)
            {
                MessageBox.Show("打开时，数据库连接失败！");
                return null;
            }
            using (SqlCommand command = new SqlCommand(sql, sc))
            {
                //command.Parameters.AddWithValue("@Userid", id);
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    List<WinFormsAppTest.pojo.Exam> exams = new List<WinFormsAppTest.pojo.Exam>();
                    while (reader.Read())
                    {
                        // 处理查询结果
                        // 例如输出数据到控制台或存储到集合中
                        int examid = reader.GetInt32(0);
                        string state = reader.GetString(1);
                        string measure = reader.GetString(2);
                        string goal = reader.GetString(3);
                        int userid = reader.GetInt32(4);
                        WinFormsAppTest.pojo.Exam f = new WinFormsAppTest.pojo.Exam(examid, state, measure, goal, userid);
                        exams.Add(f);
                    }
                    sc.Close();
                    return exams;
                }
            }
        }
        //


    }
}
